home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_structseq.py < prev    next >
Text File  |  2005-10-18  |  3KB  |  105 lines

  1. import unittest
  2. from test import test_support
  3.  
  4. import time
  5.  
  6. class StructSeqTest(unittest.TestCase):
  7.  
  8.     def test_tuple(self):
  9.         t = time.gmtime()
  10.         astuple = tuple(t)
  11.         self.assertEqual(len(t), len(astuple))
  12.         self.assertEqual(t, astuple)
  13.  
  14.         # Check that slicing works the same way; at one point, slicing t[i:j] with
  15.         # 0 < i < j could produce NULLs in the result.
  16.         for i in xrange(-len(t), len(t)):
  17.             self.assertEqual(t[i:], astuple[i:])
  18.             for j in xrange(-len(t), len(t)):
  19.                 self.assertEqual(t[i:j], astuple[i:j])
  20.  
  21.         for j in xrange(-len(t), len(t)):
  22.             self.assertEqual(t[:j], astuple[:j])
  23.  
  24.         self.assertRaises(IndexError, t.__getitem__, -len(t)-1)
  25.         self.assertRaises(IndexError, t.__getitem__, len(t))
  26.         for i in xrange(-len(t), len(t)-1):
  27.             self.assertEqual(t[i], astuple[i])
  28.  
  29.     def test_repr(self):
  30.         t = time.gmtime()
  31.         repr(t)
  32.  
  33.     def test_concat(self):
  34.         t1 = time.gmtime()
  35.         t2 = t1 + tuple(t1)
  36.         for i in xrange(len(t1)):
  37.             self.assertEqual(t2[i], t2[i+len(t1)])
  38.  
  39.     def test_repeat(self):
  40.         t1 = time.gmtime()
  41.         t2 = 3 * t1
  42.         for i in xrange(len(t1)):
  43.             self.assertEqual(t2[i], t2[i+len(t1)])
  44.             self.assertEqual(t2[i], t2[i+2*len(t1)])
  45.  
  46.     def test_contains(self):
  47.         t1 = time.gmtime()
  48.         for item in t1:
  49.             self.assert_(item in t1)
  50.         self.assert_(-42 not in t1)
  51.  
  52.     def test_hash(self):
  53.         t1 = time.gmtime()
  54.         self.assertEqual(hash(t1), hash(tuple(t1)))
  55.  
  56.     def test_cmp(self):
  57.         t1 = time.gmtime()
  58.         t2 = type(t1)(t1)
  59.         self.assertEqual(t1, t2)
  60.         self.assert_(not (t1 < t2))
  61.         self.assert_(t1 <= t2)
  62.         self.assert_(not (t1 > t2))
  63.         self.assert_(t1 >= t2)
  64.         self.assert_(not (t1 != t2))
  65.  
  66.     def test_fields(self):
  67.         t = time.gmtime()
  68.         self.assertEqual(len(t), t.n_fields)
  69.         self.assertEqual(t.n_fields, t.n_sequence_fields+t.n_unnamed_fields)
  70.  
  71.     def test_constructor(self):
  72.         t = time.struct_time
  73.  
  74.         self.assertRaises(TypeError, t)
  75.         self.assertRaises(TypeError, t, None)
  76.         self.assertRaises(TypeError, t, "123")
  77.         self.assertRaises(TypeError, t, "123", dict={})
  78.         self.assertRaises(TypeError, t, "123456789", dict=None)
  79.  
  80.         s = "123456789"
  81.         self.assertEqual("".join(t(s)), s)
  82.  
  83.     def test_eviltuple(self):
  84.         class Exc(Exception):
  85.             pass
  86.  
  87.         # Devious code could crash structseqs' contructors
  88.         class C:
  89.             def __getitem__(self, i):
  90.                 raise Exc
  91.             def __len__(self):
  92.                 return 9
  93.  
  94.         self.assertRaises(Exc, time.struct_time, C())
  95.  
  96.     def test_reduce(self):
  97.         t = time.gmtime()
  98.         x = t.__reduce__()
  99.  
  100. def test_main():
  101.     test_support.run_unittest(StructSeqTest)
  102.  
  103. if __name__ == "__main__":
  104.     test_main()
  105.